<template>
    <!-- 选择框 -->
    <PopList :title="'文字标注'">
        <div class="roam_moveOnLineMode">
            <div>
                <label for="">连续创建</label><span class="roam_moveOnLineMode_checke"
                    :class="{ 'roam_moveOnLineMode_checke_active': continuousCreate }" @click="changeCheckBox">
                    <es-icon :name="continuousCreate ? 'duigou' : ''" :color="'white'" :size="12" />
                </span>
            </div>
        </div>
        <div class="images_img_list">
            <div v-for="(item, index) in textType" class="images_img_lilist">
                <div class="images_imgposition" @click.stop="select(item)"
                    :class="{ 'images_checkedactive': selected && item.name === selected.name }"
                    @mouseenter="editingIconIsShow = index" @mouseleave="editingIconIsShow = -1">
                    <!-- <img :src="item.img" alt=""> -->

                    <div class="images_imgposition_text"
                        :style="{ color: `rgba(${item.type.textColor[0] * 255},${item.type.textColor[1] * 255},${item.type.textColor[2] * 255},${item.type.textColor[3]})`, fontSize: `${item.type.textSize}px` }"
                        v-if="!(textareaIsShow === index)">{{ item.text }}</div>
                    <textarea name=text class="images_imgposition_textarea" v-model="item.text" @click.stop
                        :style="{ color: `rgba(${item.type.textColor[0] * 255},${item.type.textColor[1] * 255},${item.type.textColor[2] * 255},${item.type.textColor[3]})` }"
                        @blur="textareaIsShow = -1" @keydown.enter="textareaIsShow = -1" v-else></textarea>
                    <!-- <span v-show="iconIsShow === index">{{ item.name ?? '模式' }}</span> -->
                    <div class="span2" @click.stop.prevent="changeText(item, index)"
                        @mouseenter="currentIconIndex = index" @mouseleave="currentIconIndex = -1"
                        v-show="editingIconIsShow === index"><es-icon :name="'bianji'"
                            :color="index === currentIconIndex ? 'rgba(44, 104, 247, 1)' : 'rgba(230, 230, 230, 1)'"
                            :size="12" /></div>
                </div>
                <!-- <div class="images_onlineimageName" @mouseenter="iconIsShow = index" @mouseleave="iconIsShow = null">{{
        item.name ?? '模式' }}</div> -->
            </div>
        </div>

    </PopList>
</template>

<script setup lang="ts">
import { ESTextLabel } from "esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main";
import { ref, onBeforeUnmount, onMounted, inject } from "vue";
import PopList from "../../../components/PopList.vue";
import { createSceneObjTreeItemFromJson, executePos } from "./fun";
import { textTypeList } from "../data";
import { XbsjEarthUi } from "../../../scripts/xbsjEarthUi";
const xbsjEarthUi = inject('xbsjEarthUi') as XbsjEarthUi
// const iconIsShow: any = ref()//黑色全程的显影
const editingIconIsShow = ref(-1)//编辑的小icon显影
const currentIconIndex = ref(-1)//编辑的小icon的颜色
const textareaIsShow = ref<number>(-1)//是否出现编辑框
// const textareaText = ref('请输入文本')
const continuousCreate = ref(false)//连续创建
const textType = ref(textTypeList)//类型列表
const selected: any = ref(textTypeList[0])//选中的类型列表
const flag = ref(false)
let sceneObject: ESTextLabel | undefined = undefined
const pos = (position: [number, number, number]) => {
    flag.value = true
    if (!selected.value) return
    if (!continuousCreate.value) return
    flag.value = false
    createSceneObject(position)
}
const changeCheckBox = () => {//点击取消连续创建时使得文字标注类型为空
    if (sceneObject) {
        sceneObject.editing = false
    }
    if (continuousCreate.value) {
        selected.value = undefined
    }
    continuousCreate.value = !continuousCreate.value
    flag.value = false
}
const select = (item: { name: string; text: string; type: { textSize: number, textColor: [number, number, number, number] } }) => {//点击选择框中的文字标注按钮
    selected.value = item
    if (continuousCreate.value) return
    createOneSceneObject()
}
const changeText = (item: { name: string; text: string; type: { textSize: number, textColor: [number, number, number, number] } }, index: number) => {
    textareaIsShow.value = index

}
//创建文字标注场景对象
const createSceneObject = (position: [number, number, number]) => {
    if (!selected.value) return
    const obj = xbsjEarthUi.createSceneObject(ESTextLabel) as ESTextLabel
    obj.position = position
    obj.fontSize = selected.value.type.textSize
    obj.color = selected.value.type.textColor
    obj.name = selected.value.name
    obj.text = selected.value.text
    //编辑状态结束后根据json创建在场景树上
    obj.editing = true
    obj.editingChanged.disposableOnce(() => {
        if (obj.editing === false) {
            const json = JSON.parse(obj.jsonStr)
            // obj.destroy()
            xbsjEarthUi.destroySceneObject(obj)
            createSceneObjTreeItemFromJson(xbsjEarthUi, json)
        }
    })
}
//创建单个报警场景对象
const createOneSceneObject = () => {
    if (!selected.value) return
    const obj2 = xbsjEarthUi.createSceneObject(ESTextLabel) as ESTextLabel
    obj2.fontSize = selected.value.type.textSize
    obj2.color = selected.value.type.textColor
    obj2.name = selected.value.name
    obj2.text = selected.value.text

    //编辑状态结束后根据json创建在场景树上
    obj2.editing = true
    sceneObject = obj2
    sceneObject.d(sceneObject.editingChanged.disposableWeakOn(() => {
        if (sceneObject && sceneObject.editing === false) {
            const json = JSON.parse(sceneObject.jsonStr)
            const position = sceneObject.position
            const a = position[0] === 0 && position[1] === 0
            xbsjEarthUi.destroySceneObject(sceneObject)
            sceneObject = undefined
            setTimeout(() => {
                if (!a && flag.value) {
                    createSceneObjTreeItemFromJson(xbsjEarthUi, json)
                    flag.value = false
                    selected.value = undefined
                }
            }, 100)
        }
    }))
}
onMounted(() => {
    createOneSceneObject()
    const disposes = executePos(xbsjEarthUi, pos)
    onBeforeUnmount(() => {
        if (disposes) {
            disposes.forEach((item) => {
                item && item()
            })
        }
        // if (disposes) {
        //     disposes()
        // }
    })
})
</script>
